package  {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.*;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.utils.getTimer;
	
	[SWF(width="600", height="600", frameRate="60")]
	public class Test extends Sprite{
		
		private var _domain:ApplicationDomain;
		private var _context:LoaderContext;
		
		private var _loader:Loader;
		
		private const COUNT:int = 1000;
		
		private const ROW:int = Math.sqrt(COUNT);
		
		private var _dic:Array;
		
		private var _bm:Bitmap;
		
		private var _index:int;
		
		private var _profilerManager:ProfilerManager;
		
		private var _loadInfoFrameRate:int;
		
		public function Test() {		
			init();
		}
		
		private function init():void{
			_dic = [];
			
			loadSource();
			initView();
			initEvent();
		}
		private function loadSource():void{
			_domain = new ApplicationDomain(ApplicationDomain.currentDomain);
			_context = new LoaderContext(true);
			_context.applicationDomain = _domain;
			
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __loadComplete);
			_loader.load(new URLRequest("../lib/Living.swf"), _context);
		}
		
		private function __loadComplete(e:Event):void{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __loadComplete);
			_loadInfoFrameRate = _loader.contentLoaderInfo.frameRate;
			
			var assetName:String = "game.ui.animation.Living210012";
//			for(var i:int = 0; i < COUNT; i++){
				if(_domain.hasDefinition(assetName))
				{
					var cls:Class = _domain.getDefinition(assetName) as Class;
					var mc:MovieClip = new cls();

					for(var i:int = 1; i <= mc.totalFrames; i++){
						mc.gotoAndStop(i);
						_dic.push(rasterizeFrame(mc));
					}
//					mc.gotoAndStop(62);
//					advanceChild(mc, 1);
//					_bm = new Bitmap(rasterizeFrame(mc));
					this.addEventListener(Event.ENTER_FRAME, __frame);
				}
//				mc.x = (i % 10) * 100 - 250;
//				mc.y = (i / 10) * 60 - 250;
//				this.addChild(mc);
//			}
		}
		
		private function initView():void{
			_bm = new Bitmap();
			this.addChild(_bm);
			
			_profilerManager = new ProfilerManager(stage);
			_profilerManager.toggleDisplay();
		}
		
		private function initEvent():void{
			
		}
		
		private function __frame(e:Event):void{
			var ratio:Number = _loadInfoFrameRate / stage.frameRate;
//			if(ratio < 1){
//				ratio = 1;
//			}
			if(_bm.bitmapData){
				_bm.bitmapData.dispose();
			}
			_bm.bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFFFFFFFF);
			
			var mae:int = getTimer();
			
			for(var i:int = 0; i < COUNT; i++){
//				_bm.bitmapData = _dic[_index] as BitmapData;
//				_bm.y = 113.15 - _bm.height;
				_bm.bitmapData.lock();
				var bd:BitmapData = _dic[int(_index * ratio)] as BitmapData;
				_bm.bitmapData.copyPixels(bd, bd.getColorBoundsRect(0xFF000000, 0x00000000, true), new Point((stage.stageWidth / ROW) * (i % ROW), (stage.stageHeight / ROW) * int((i / ROW))), null, null, true);
				_bm.bitmapData.unlock();
			}
			
			_index++;
			if(_index >= _dic.length / ratio){
				_index = 0;
			}
			_profilerManager.draw();
		}
		
		private function advanceChild(parent:MovieClip, index:int):void {
			for (var i:int = 0; i < parent.numChildren; i++) {
				var mc:MovieClip = parent.getChildAt(i) as MovieClip;
				if (! mc){
					continue;
				}
				if (mc.totalFrames > mc.currentFrame)
					mc.gotoAndStop(mc.currentFrame + 1);
				else 
					mc.gotoAndStop(1);
				
				advanceChild(mc, index);
			}
		}
		
		private function rasterizeFrame(mc:MovieClip):BitmapData
		{
			var bd:BitmapData = new BitmapData(mc.getBounds(mc).width, mc.getBounds(mc).height, true, 0x0);
			var m:Matrix = new Matrix();
			//将mc移动到0,0点
			m.translate(-mc.getBounds(mc).x, -mc.getBounds(mc).y);
			
			bd.draw(mc, m, null, null, null, true);
			return bd;
		}
	}
	
}
